/**
 * 
 */

package com.shy.portal.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.shy.base.controller.AbsController;
import com.shy.base.vo.AppConstants;
import com.shy.base.vo.EasyuiPagerVo;
import com.shy.base.vo.PagerFilter;
import com.shy.portal.entity.Menu;
import com.shy.portal.entity.Role;
import com.shy.portal.entity.RoleMenu;
import com.shy.portal.service.MenuService;
import com.shy.portal.service.RoleMenuService;
import com.shy.portal.service.RoleService;
import com.shy.portal.utils.Data2TreeVoUtils;
import com.shy.portal.vo.RoleMenuVo;

/**
 * <p>
 * Title: RoleController
 * </p>
 * <p>
 * Description:角色信息管理控制器
 * </p>
 * <p>
 * Company:ipebg
 * </p>
 * 
 * @author H2013788
 */
@Controller
@RequestMapping("/role_menu")
public class RoleMenuController extends AbsController {

	/**
     * log
     */
	private Logger logger = LogManager.getLogger(this.getClass());
    
    /**
     * 角色service层
     */
    @Resource
    private RoleService roleService;
    /**
     * 菜单service层
     */
    @Resource
    private MenuService menuService;
    
    /**
     * 角色菜单service层
     */
    @Resource
    private RoleMenuService roleMenuService;

    /**
     * 编辑角色权限，查询菜单列表
     * @param roleId 角色ID
     * @param response 反饋
     * @author H2201965
     * @date 2018/2/6下午2:50:07
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/load_role_menu")
    public void editRoleMenu(Integer roleId, HttpServletResponse response) {
        // 查询最大菜单级別数值，用於拼裝樹形数据
        int maxLevel = menuService.getMaxLevel();
        // 所有菜单列表
        PagerFilter pagerFilter = getPageableByNoPage();
        pagerFilter = menuService.listByPager(null, null, pagerFilter);
        List<Menu> menuList = pagerFilter.getObjs();
        List<RoleMenuVo> list = new ArrayList<RoleMenuVo>();
        if (null != roleId) {
            List<Menu> roleMenuList = menuService.getMenusByRoleId(roleId);
            list = Data2TreeVoUtils.buildMenuTreeVo(maxLevel, roleMenuList, menuList);
        } else {
            list = Data2TreeVoUtils.buildMenuTreeVo(maxLevel, null, menuList);
        }
        writeResponse(new StringBuffer(gsonDateTime.toJson(list)), response);
    }

    /**
     * 保存角色菜单
     * @param menuIds 菜单ID
     * @param roleId 角色ID
     * @param response 反饋
     * @author H2201965
     * @date 2018/2/6下午2:50:30
     */
    @RequestMapping("/save_role_menu")
    public void saveRoleMenu(Integer[] menuIds, Integer roleId, HttpServletResponse response) {
        boolean result = false;
        if (null != menuIds && menuIds.length > 0) {
            List<RoleMenu> list = new ArrayList<>();
            RoleMenu roleMenu = null;
            Role role = new Role();
            role.setId(roleId);
            for (Integer menuId : menuIds) {
                roleMenu = new RoleMenu();
                roleMenu.setRole(role);
                Menu menu = new Menu();
                menu.setId(menuId);
                roleMenu.setMenu(menu);
                roleMenu.setCreateTime(new Date());
                roleMenu.setChecked(true);
                list.add(roleMenu);
            }
            try {
                result = roleMenuService.batchSaveRoleMenu(list, roleId);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                logger.error(AppConstants.configMap.get(AppConstants.SAVE_ERROR) + e);
            }
        }
        if (result) {
            respEasyuiSuccessResult(AppConstants.configMap.get(AppConstants.SAVE_SUCCESS), response);
        } else {
            respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.SAVE_ERROR), response);
        }
    }

}
